<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>消息-云校通</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
</head>
<body ontouchstart id="xs">
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui_cells mt0" id="messageList">
            	<c:forEach var="item" items="${notices }">
            		<c:choose>
            			<c:when test="${item.noticeType == 3 }">
            				<a href="<%=basePath %>/ls/notice/fileAround" style="display: block;">
		            			<div class="weui_cell slidelefts p10">
				                    <div class="weui_media_hd" style="width: 20%;">
				                        <img class="weui_left_img" style="width: 90%;" src="<%=basePath %>/images/notice_${item.noticeType }.png" alt="">
				                    </div>
				                    <div class="weui_cell_bd weui_cell_primary weui_media_box borderNoneBefore">
				                        <h4 class="weui_media_title">
				                        	${item.noticeTypeText}
				                        </h4>
				                        <p class="xs-time"><fmt:formatDate value="${item.createTime}" type="time" timeStyle="short"/></p>
				                        <p class="weui_media_desc f13">${item.content }</p>
				                        <c:if test="${item.count > 0 }"><span class="xs-num">${item.count }</span></c:if>
				                    </div>
				                </div>
			                </a>
            			</c:when>
            			<c:otherwise>
            				<a href="<%=basePath %>/ls/notices/list?nt=${item.noticeType}" style="display: block;">
            					<div class="weui_cell slidelefts p10">
				                    <div class="weui_media_hd">
				                        <img class="weui_left_img" src="<%=basePath %>/images/notice_${item.noticeType }.png" alt="">
				                    </div>
				                    <div class="weui_cell_bd weui_cell_primary weui_media_box borderNoneBefore">
				                        <h4 class="weui_media_title">
				                        	${item.noticeTypeText}
				                        </h4>
				                        <p class="xs-time"><fmt:formatDate value="${item.createTime}" type="time" timeStyle="short"/></p>
				                        <p class="weui_media_desc f13">${item.content }</p>
				                        <c:if test="${item.count > 0 }"><span class="xs-num">${item.count }</span></c:if>
				                    </div>
				                </div>
				            </a>
            			</c:otherwise>
            		</c:choose>
            	</c:forEach>
            </div>
        </div>
	</div>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script>
	$(function() {
		var userRole = "${user.roleType}";
		var reqData = getReqChatList(userRole);
		$.ajax({
          	url : basePath + "/message/chat",
  	 		type : "post",
  	 		data : JSON.stringify(reqData),
		    contentType: "application/json",
		    dataType : 'json',
		    async:false,
            success: function(data){
            	var chatData = getChatList(userRole);
            	var html = "";
            	if(data != null && data.length > 0) {
            		for(var i in data) {
            			html += loadHtml(data[i])
                        for(var j in chatData) {
                        	if(data[i].chatType == chatData[j].chatType) {
                        		if(data[i].chatType == 'group' && (data[i].schoolId = chatData[j].schoolId 
                                && data[i].grade == chatData[j].grade && data[i].clazz == chatData[j].clazz)) {
                        			chatData.shift(j);
                        			break;
                        		}else if(data[i].chatType == 'private' && (data[i].schoolId = chatData[j].schoolId 
                              		  && data[i].grade == chatData[j].grade && data[i].clazz == chatData[j].clazz
                              		  && data[i].studentId == chatData[j].studentId)) {
                        			chatData.shift(j);
                        			break;
                        		}
                        	}
                        }
            		}
            	}
        		for(var j in chatData) {
        			html += loadHtml1(chatData[j])
        		}
        		$("#messageList").append(html);
        		loadLeft();
            },
            error: function(xhr, type){
              
            }
          }); 
	});
	function loadHtml(item) {
		if(!item.message) return '';
		var html = "";
		var getKey = "";
		if(item.chatType == 'group') {
			getKey = getGroupChatKey("${user.roleType}", item.chatType, item.schoolId, item.grade, item.clazz);
		}else if(item.chatType == 'private') {
			getKey = getPrivateKey("${user.roleType}", item.chatType, item.schoolId, item.grade, item.clazz, item.studentId);
		}
		html += '<a style="display:block;" id="'+getKey+'" href="<%=basePath%>'+ (item.chatType == 'group' ? '/ls/groupChat/class?grade='+ item.grade +'&clazz='+ item.clazz : '/ls/groupChat/class/sx?grade='+ item.grade +'&clazz='+ item.clazz + '&sid=' + item.studentId) +'">';
		html += '<div class="weui_cell slidelefts pt5 pb5">';
		html += '<div class="weui_media_hd">';
        html += '<img class="weui_left_img" src="<%=basePath%>/images/siliao_jiazhang@3x.png" alt="">';
        html += '</div>';
        html += '<div class="weui_cell_bd weui_cell_primary weui_media_box borderNoneBefore">';
        html += '<h4 class="weui_media_title">';
        if(item.chatType == 'group') html += item.gradeName+'（'+item.clazz+'）班 留言板';
        else if (item.chatType == 'private') html += item.studentName + ' 家长';
        html += '</h4>';
        html += '<p class="xs-time">'+ getDateDiff(item.createTime, 'yyyy/MM/dd')+'</p>';
        html += '<p class="weui_media_desc f13">'+ (item.messageType=='image' ? '[图片]' : item.message) +'</p>';
        html += '<span class="xs-num">'+(item.count > 99 ? '99+' : item.count)+'</span>';
        html += '</div></div></a>';
        return html;
	}
	function loadHtml1(item) {
		if(!item.message){
			item.message = "";
		}
		var html = "";
		var getKey = "";
		if(item.chatType == 'group') {
			getKey = getGroupChatKey("${user.roleType}", item.chatType, item.schoolId, item.grade, item.clazz);
		}else if(item.chatType == 'private') {
			getKey = getPrivateKey("${user.roleType}", item.chatType, item.schoolId, item.grade, item.clazz, item.studentId);
		}
		html += '<a style="display:block;" id="'+getKey+'" href="<%=basePath%>'+ (item.chatType == 'group' ? '/ls/groupChat/class?grade='+ item.grade +'&clazz='+ item.clazz : '/ls/groupChat/class/sx?grade='+ item.grade +'&clazz='+ item.clazz + '&sid=' + item.studentId) +'">';
		html += '<div class="weui_cell slidelefts pt5 pb5">';
		html += '<div class="weui_media_hd">';
        html += '<img class="weui_left_img" src="<%=basePath%>/images/siliao_jiazhang@3x.png" alt="">';
        html += '</div>';
        html += '<div class="weui_cell_bd weui_cell_primary weui_media_box borderNoneBefore">';
        html += '<h4 class="weui_media_title">';
        if(item.chatType == 'group') html += getGradeName("${term.termKey}", item.grade)+'（'+item.clazz+'）班 留言板';
        else if (item.chatType == 'private') html += item.studentName + ' 家长';
        html += '</h4>';
        html += '<p class="xs-time">'+getDateDiff(item.messageDate, 'yyyy/MM/dd')+'</p>';
        html += '<p class="weui_media_desc f13">'+ item.message +'</p>';
        html += '</div></div></a>';
        return html;
	}
	function delMessage(thiz) {
		obj[0].href="javascript:;"
		var obj = $(thiz).closest("a");
		window.localStorage.removeItem(obj.attr("id"));
		obj.remove();
	}
</script>
</body>
</html>